package com.czt.mail.controller;

import com.czt.mail.exception.BusinessException;
import com.czt.mail.util.MailMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author czt
 * @version 1.0
 * @date 2024/7/18 13:57
 * {@desc}
 */
@RestController
@RequestMapping("/mail")
@Slf4j
public class MailController {

    @Autowired
    private MailMsg mailMsg;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/test/{id}")
    public String test(@PathVariable String id) {
        System.out.println("id = " + id);
        if (id.equals("1")) {
            throw new BusinessException("123", "哈哈抓住你了");
        }
        return "mail";
    }

    @GetMapping("/{mail}")
    public String sendMailCode(@PathVariable String mail) throws Exception{
        log.info("邮箱为:{}",mail);
        String code = redisTemplate.opsForValue().get(mail);
        if (code != null) {
            return "请求频繁";
        }

        boolean flag = mailMsg.mail(mail);

        if (!flag) {
            return "验证码发送失败";
        }

        return "验证码发送成功";
    }
}
